package com.demoo.zjsu.service.smartwebsite;

import com.demoo.zjsu.dao.smartwebsite.SmartWebDAO;
import com.demoo.zjsu.model.smartwebsite.PageOne;
import com.demoo.zjsu.util.smartwebsite.WebsiteUtil;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by jshjsh on 2016/8/16.
 * 适用于一对一的基础界面
 */
@Service
public abstract class BaseFactory extends IFactory{

    @Autowired
    SmartWebDAO dao;

    public void resolver() {
       Page page = getPage();
        String[] urls = page.getLinkUrls();
        String[] category = page.getCategories();
        List<PageOne> pageOnes = new ArrayList<PageOne>();
        int size = urls.length;
        for (int i = 0; i < size; i++) {
            Document document = WebsiteUtil.getDocByURL(urls[i]);
            Elements elements = getMain(document);
            filterElse(elements);
            filterMain(elements);
            dealPhotoAndFile(elements);
            PageOne pageOne = getPageOne(elements);
            pageOne.setCategory(category[i]);
            pageOnes.add(pageOne);
        }
        dao.insertListPageOne(pageOnes);
    }

    public abstract PageOne getPageOne(Elements e);

    @Override
    public void addPhoto(Elements elements) {

    }

    @Override
    public void addFile(Elements elements) {

    }
}
